package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.TccbB2bManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.TccbBankStatus;
import com.gopay.common.constants.bank.TccbContants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.tccb.ebank.TccbB2bMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

public class TccbB2bBankServiceImpl implements BankInteractRemoteService {

	@Resource(name = "tccbB2bManager")
	private TccbB2bManager tccbB2bManager;

	@Override
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
		try {
			TccbB2bMessage messageB2B = new TccbB2bMessage();
			base.setBankOrderId(base.getOrderId());
			BeanUtils.copyProperties(base,messageB2B);
			TccbB2bMessage resultMessage = (TccbB2bMessage)tccbB2bManager.sign(messageB2B);
			result.setDefaultModel(resultMessage);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
		}
		return result;
	}
	
	@Override
	public Result bankBack(BaseMessage baseMessage) {
		Result result = new Result(false);        
        try {
        	boolean flag = false;
			TccbB2bMessage messageB2B = new TccbB2bMessage();
			BeanUtils.copyProperties(baseMessage,messageB2B);
            flag = (Boolean)tccbB2bManager.verify(messageB2B);
            result.setModel("message", messageB2B);
            result.setDefaultModel(flag);
            result.setSuccess(true);
            // 验签成功入账
            if (result.isSuccess() && flag) {                
            	Map<String, String> param = new HashMap<String, String>();
                if (TccbContants.TRAN_TCCB_DEAL_SUCC.equals(messageB2B.getRespCode())) {
                	// 银行返回成功状态
                    param.put("orderId", messageB2B.getOrderId());
                    param.put("amt", messageB2B.getAmount().toString());
                    param.put("cardType", "Unknown");
                    param.put("notifyNum", "1");
                    param.put("payStatus", "00");
                    param.put("retCode", "00");
                    param.put("retMsg", "验签成功");
                    param.put("signData", messageB2B.getSignMessage());
                    param.put("isFront", baseMessage.isFront()+"");
                    result.setModel("isNotifyCashier", param);
                } else {
                    // 通知后台地址该订单失败
                    param.put("orderId", messageB2B.getOrderId());
                    param.put("amt", messageB2B.getAmount().toString());
                    param.put("cardType", "Unknown");
                    param.put("notifyNum", "1");
                    param.put("payStatus", "01");
                    param.put("retCode", "01");
                    param.put("retMsg", "验签失败");
                    param.put("signData", messageB2B.getSignMessage());
                    param.put("isFront", baseMessage.isFront()+"");
                    result.setModel("isNotifyCashier", param);
                }
            }
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
        }
        return result;
	}

	@Override
	public Result query(BaseMessage base) {
		 Result result = new Result(false);
	        TccbB2bMessage messageB2B = new TccbB2bMessage();
	        BeanUtils.copyProperties(base, messageB2B);
	        try {
	        	messageB2B = (TccbB2bMessage)tccbB2bManager.query(messageB2B);
		        result.setDefaultModel(messageB2B);
		        result.setSuccess(true);	        
		        if(BisPayCheckUtils.checkExternalOrderID(base.getOrderId())){
		    		Map<String, String> param = new HashMap<String, String>();
		    		param.put("orderId", base.getOrderId());
		    		param.put("amount", base.getAmount().toString());
		    		if (messageB2B == null) {
		    			// 查询订单为空
		    			param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
		    			param.put("returnMessage", "银行返回对账结果为空");
		    			param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						result.setModel("queryResult", param);
						return result;
					}

					if (!TccbContants.QUERY_PROC_STATUS_SUCC.equals(messageB2B.getRespCode())) {
						// 查询订单错误
		    			param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
		    			param.put("returnMessage", "银行返回对账结果为未查到相关订单");
		    			param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						result.setModel("queryResult", param);
						return result;
					}
					//成功处理
					if (StringUtils.equals(TccbContants.TRAN_TCCB_DEAL_SUCC,messageB2B.getCompFlag())) {
	    				param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
    					param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
    					param.put("returnMessage", "交易成功");
					}
					//失败处理
					else if(StringUtils.equals(TccbContants.TRAN_TCCB_DEAL_FAIL,messageB2B.getCompFlag())){
						String errorMsg = TccbBankStatus.getName(messageB2B.getCompFlag());
	    				param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
    					param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
    					param.put("returnMessage", messageB2B.getCompFlag() + " - "+ errorMsg);
					}else {
						String errorMsg = TccbBankStatus.getName(messageB2B.getCompFlag());
	    				param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
    					param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
    					param.put("returnMessage", messageB2B.getCompFlag() + " - "+ errorMsg);
					}
		    		result.setModel("queryResult", param);
		    	}
			}catch (GopayException e) {
				BankLogUtils.printServiceImplExceptionLog(e);
	    		result.setResultCode(e.getErrCode());
			}catch(Exception ex){
				BankLogUtils.printServiceImplExceptionLog(ex);
				result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
			}
	        return result;
	}
	
	@Override
	public Result dayend(BaseMessage arg0) {
		return null;
	}

	@Override
	public Result refund(BaseMessage arg0) {
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
